﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections.Generic;
using System.Windows;

namespace EntLib6DAABInCode
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //1.
            //var employeeManagers = new List<EmployeeManager>();
            //var database = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase("Data source=.;User ID=sa; Password=sql; Initial Catalog=AdventureWorks2012");
            //using (var dataReader = database.ExecuteReader("dbo.uspGetEmployeeManagers", 81))
            //{
            //    while (dataReader.Read())
            //        employeeManagers.Add(new EmployeeManager
            //        {
            //            FirstName = dataReader.GetString(2),
            //            LastName = dataReader.GetString(3),
            //            OrganizationNode = dataReader.GetString(4),
            //            ManagerFirstName = dataReader.GetString(5),
            //            ManagerLastName = dataReader.GetString(6)
            //        });
            //}
            //this.ManagersDataGrid.ItemsSource = employeeManagers;

            //2.
            //var employeeManagers = new List<EmployeeManager>();
            //var database = DatabaseFactory.CreateDatabase("AdventureWorks");
            //using (var dataReader = database.ExecuteReader("dbo.uspGetEmployeeManagers", 81))
            //{
            //    while (dataReader.Read())
            //        employeeManagers.Add(new EmployeeManager
            //        {
            //            FirstName = dataReader.GetString(2),
            //            LastName = dataReader.GetString(3),
            //            OrganizationNode = dataReader.GetString(4),
            //            ManagerFirstName = dataReader.GetString(5),
            //            ManagerLastName = dataReader.GetString(6)
            //        });
            //}
            //this.ManagersDataGrid.ItemsSource = employeeManagers;

            //3.
            var employeeManagers = new List<EmployeeManager>();
            var database = DatabaseFactory.CreateDatabase();
            using (var dataReader = database.ExecuteReader("dbo.uspGetEmployeeManagers", 81))
            {
                while (dataReader.Read())
                    employeeManagers.Add(new EmployeeManager
                    {
                        FirstName = dataReader.GetString(2),
                        LastName = dataReader.GetString(3),
                        OrganizationNode = dataReader.GetString(4),
                        ManagerFirstName = dataReader.GetString(5),
                        ManagerLastName = dataReader.GetString(6)
                    });
            }
            this.ManagersDataGrid.ItemsSource = employeeManagers;
        }
    }

    public class EmployeeManager
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string OrganizationNode { get; set; }

        public string ManagerFirstName { get; set; }

        public string ManagerLastName { get; set; }
    }
}